<?php

/**
 * @file classes/manager/form/CTKTForm.inc.php
 *
 * Copyright (c) 2003-2012 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @class CTKTForm
 * @ingroup manager_form
 *
 * @brief Form for creating and modifying journal CTKTs.
 */

// $Id$


import('lib.pkp.classes.form.Form');

class CTKTForm extends Form {

	/** @var $CTKTId int The ID of the CTKT being edited */
	var $CTKTId;

	/** @var $includeCTKTEditor object Additional CTKT editor to
	 *       include in assigned list for this CTKT
	 */
	var $includeCTKTEditor;

	/** @var $omitCTKTEditor object Assigned CTKT editor to omit from
	 *       assigned list for this CTKT
	 */
	var $omitCTKTEditor;

	/** @var $CTKTEditors array List of user objects representing the
	 *       available CTKT editors for this journal.
	 */
	var $CTKTEditors;

	/**
	 * Constructor.
	 * @param $journalId int omit for a new journal
	 */
	function CTKTForm($CTKTId = null) {
		parent::Form('acctCN/CTKTForm.tpl');

		$journal =& Request::getJournal();
		$this->CTKTId = $CTKTId;

		// Validation checks for this form
		$this->addCheck(new FormValidatorLocale($this, 'matk', 'required', 'Phai co Ma TK!'));
		$this->addCheck(new FormValidatorLocale($this, 'tentk', 'required', 'Phai co Ten TK'));
		$this->addCheck(new FormValidatorPost($this));
		

		// Get a list of CTKT editors for this journal.
		$roleDao =& DAORegistry::getDAO('RoleDAO');
		$this->CTKTEditors =& $roleDao->getUsersByRoleId(ROLE_ID_CTKT_EDITOR, $journal->getId());
		$this->CTKTEditors =& $this->CTKTEditors->toArray();
		
	}

	/**
	 * When displaying the form, include the specified CTKT editor
	 * in the assigned list for this CTKT.
	 * @param $CTKTEditorId int
	 */
	function includeCTKTEditor($CTKTEditorId) {
		foreach ($this->CTKTEditors as $key => $junk) {
			if ($this->CTKTEditors[$key]->getId() == $CTKTEditorId) {
				$this->includeCTKTEditor =& $this->CTKTEditors[$key];
			}
		}
	}

	/**
	 * When displaying the form, omit the specified CTKT editor from
	 * the assigned list for this CTKT.
	 */
	function omitCTKTEditor($CTKTEditorId) {
		foreach ($this->CTKTEditors as $key => $junk) {
			if ($this->CTKTEditors[$key]->getId() == $CTKTEditorId) {
				$this->omitCTKTEditor =& $this->CTKTEditors[$key];
			}
		}
	}

	/**
	 * Get the names of fields for which localized data is allowed.
	 * @return array
	 */
	function getLocaleFieldNames() {
		$CTKTDao =& DAORegistry::getDAO('CTKTDAO');
		return $CTKTDao->getLocaleFieldNames();
	}

	/**
	 * Display the form.
	 */
	function display() {
		$journal =& Request::getJournal();
		$templateMgr =& TemplateManager::getManager();

		$templateMgr->assign('CTKTId', $this->CTKTId);
		
		parent::display();
	}

	/**
	 * Initialize form data from current settings.
	 */
	function initData() {
		$journal =& Request::getJournal();
		if (isset($this->CTKTId)) {
			$CTKTDao =& DAORegistry::getDAO('CTKTDAO');
			$CTKT =& $CTKTDao->getCTKT($this->CTKTId, false, false);
			if ($CTKT == null) {
				unset($this->CTKTId);
			} else {
				$this->_data = array(
					'matk' => $CTKT->getmatk(), // Localized
					'tentk' => $CTKT->gettentk(), // Localized
				);
			}
		}
	}

	/**
	 * Assign form data to user-submitted data.
	 */
	function readInputData() {
		$this->readUserVars(array('matk', 'tentk'));
	}

	/**
	 * Save CTKT.
	 */
	function execute() {

		$CTKTDao =& DAORegistry::getDAO('CTKTDAO');

		if (isset($this->CTKTId)) {
			$CTKT =& $CTKTDao->getCTKT($this->CTKTId, false, false);
		}

		if (!isset($CTKT)) {
			$CTKT = new CTKT();
		}

		$CTKT->setmatk($this->getData('matk'), null); // Localized
		$CTKT->settentk($this->getData('tentk'), null); // Localized

		
		if (isset($this->CTKTId)) {
			$CTKTDao->updateCTKT($CTKT);
			$CTKTId = $CTKT->getId();

		} else {

			$CTKTId = $CTKTDao->insertCTKT($CTKT);
			
		}

	}
}

?>
